package com.koron.order.linepatrol.satellite.excel;

import cn.hutool.core.map.BiMap;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName: PerambulateStringStringConverter
 * @Description:
 * @Date: 2023/2/1
 * @Author: 李一辰
 * @Version: 1.0
 **/
@Slf4j
public class LinePatrolSatelliteSSConverter extends StringStringConverter {

    private final Map<String, BiMap<String, String>> fieldMap = new HashMap<>();
    private final String title = "$title$";

    public LinePatrolSatelliteSSConverter() {
        // 风险等级 risk_level
        BiMap<String, String> riskLevelMap = new BiMap<>(DataDictTool.getDataDictByCode("transmissionRiskControl", "risk_level"));
        riskLevelMap.put(title, "风险等级");
        // 字段和数据字典对应
        fieldMap.put("riskLevel", riskLevelMap);
    }
    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String key = cellData.getStringValue();
        if (contentProperty == null|| contentProperty.getField() == null) {
            return key;
        }
        ExcelProperty excelProperty = contentProperty.getField().getAnnotation(ExcelProperty.class);
        if (excelProperty == null || excelProperty.converter() == AutoConverter.class) {
            return key;
        }
        BiMap<String, String> entries = fieldMap.get(contentProperty.getField().getName());
        if (entries != null) {
            key = entries.getKey(cellData.getStringValue());
            if (StrUtil.isNotEmpty(cellData.getStringValue()) && StrUtil.isEmpty(key)) {
                return cellData.getStringValue();
            }
        }
        return key;
    }

    @Override
    public WriteCellData<String> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (contentProperty == null|| contentProperty.getField() == null) {
            return new WriteCellData<>(value);
        }
        ExcelProperty excelProperty = contentProperty.getField().getAnnotation(ExcelProperty.class);
        if (excelProperty.converter() != null && excelProperty.converter() != AutoConverter.class) {
            BiMap<String, String> entries = fieldMap.get(contentProperty.getField().getName());
            if (entries != null) {
                return new WriteCellData<>(entries.get(Objects.toString(value, "")));
            }
        }
        return new WriteCellData<>(value);
    }

}
